package org.jeecg.modules.demo.hydrology.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.hydrology.dto.PslcdycDto;
import org.jeecg.modules.demo.hydrology.entity.Pslcdyc;
import org.jeecg.modules.demo.hydrology.mapper.PslcdycMapper;
import org.jeecg.modules.demo.hydrology.service.IPslcdycService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jeecg.modules.system.mapper.SysDictItemMapper;
import org.jeecg.modules.system.mapper.SysDictMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 排水量异常
 * @Author: jeecg-boot
 * @Date:   2024-04-17
 * @Version: V1.0
 */
@Service
public class PslcdycServiceImpl extends ServiceImpl<PslcdycMapper, Pslcdyc> implements IPslcdycService {
    @Autowired
    private PslcdycMapper pslcdycMapper;
    @Autowired
    private SysDepartMapper sysDepartMapper;
    @Autowired
    private SysDictMapper sysDictMapper;
    @Autowired
    private SysDictItemMapper sysDictItemMapper;

    /**
     * 排水量异常-分页列表查询
     * @param page
     * @param queryWrapper
     * @return
     */
    @Override
    public IPage<PslcdycDto> getList(Page<Pslcdyc> page, QueryWrapper<Pslcdyc> queryWrapper) {
        Page<Pslcdyc> pslcdycPage = pslcdycMapper.selectPage(page, queryWrapper);
        List<PslcdycDto> collect = pslcdycPage.getRecords().stream().map(pslcdyc -> {
            PslcdycDto pslcdycDto = new PslcdycDto();
            BeanUtils.copyProperties(pslcdyc, pslcdycDto);
            QueryWrapper<SysDepart> sysDepartQueryWrapper = new QueryWrapper<>();
            sysDepartQueryWrapper.eq("org_code", pslcdyc.getStationMine());
            SysDepart sysDepart = sysDepartMapper.selectOne(sysDepartQueryWrapper);
            if (sysDepart != null){
                pslcdycDto.setStationName(sysDepart.getDepartName());
            }
            QueryWrapper<SysDict> sysDictQueryWrapper = new QueryWrapper<>();
            sysDictQueryWrapper.eq("dict_code","point_status");
            SysDict sysDict = sysDictMapper.selectOne(sysDictQueryWrapper);
            if (sysDict != null){
                QueryWrapper<SysDictItem> sysDictItemQueryWrapper = new QueryWrapper<>();
                sysDictItemQueryWrapper.eq("dict_id",sysDict.getId()).eq("item_value",pslcdyc.getAbnormalType());
                SysDictItem sysDictItem = sysDictItemMapper.selectOne(sysDictItemQueryWrapper);
                if (sysDictItem != null) {
                    pslcdycDto.setPointStatusItemText(sysDictItem.getItemText());
                }
            }
            QueryWrapper<SysDict> sysDictQueryWrapper2 = new QueryWrapper<>();
            sysDictQueryWrapper2.eq("dict_code","alarm_level");
            SysDict sysDict2 = sysDictMapper.selectOne(sysDictQueryWrapper2);
            if (sysDict2 != null){
                QueryWrapper<SysDictItem> sysDictItemQueryWrapper1 = new QueryWrapper<>();
                sysDictItemQueryWrapper1.eq("dict_id",sysDict2.getId()).eq("item_value",pslcdyc.getWarnLevel());
                SysDictItem sysDictItem1 = sysDictItemMapper.selectOne(sysDictItemQueryWrapper1);
                if (sysDictItem1 != null) {
                    pslcdycDto.setWarnLevelItemText(sysDictItem1.getItemText());
                }
            }
            return pslcdycDto;
        }).collect(Collectors.toList());
        IPage<PslcdycDto> pslcdycDtoIPage = new Page<>();
        pslcdycDtoIPage.setRecords(collect);
        pslcdycDtoIPage.setTotal(pslcdycPage.getTotal());
        pslcdycDtoIPage.setPages(page.getPages());
        pslcdycDtoIPage.setSize(page.getSize());
        return pslcdycDtoIPage;
    }
}
